package me.zhengjie.repository;

import me.zhengjie.pojo.sqlServer.PPStorage;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface PPStorageRepository extends JpaRepository<PPStorage, Integer>, JpaSpecificationExecutor<PPStorage> {


    @Query("update PPStorage ps set ps.status=?2 where ps.id=?1")
    int updatePPStorageStatusById(Integer id, Integer status);

    PPStorage findPPStorageByStorageCode(String storageCode);

    @Query("select ps from PPStorage ps where ps.status=1 and ps.locationInventoryStatus=0")
    List<PPStorage> findSuitabkePPStorage();

    @Query("select ps from PPStorage ps where ps.status=1 and ps.locationInventoryStatus=0 and ps.palletCode is not null")
    List<PPStorage> findSuitableOutNullPalletPPStorage();

    @Query("select ps.storageCode from PPStorage ps where ps.id=?1")
    String findStorageCodeById(Integer id);


    @Modifying
    @Query("update PPStorage ps set ps.status=0 where ps.storageCode=?1")
    void lockStorage(String storageCode);

    @Query("select ps from PPStorage ps where ps.status=1")
    List<PPStorage> findAllOpenStorages();

    @Modifying
    @Query("update PPStorage ps set ps.status=1,ps.locationInventoryStatus=0,ps.palletCode=null where ps.storageCode=?1")
    void cleanStorage(String storageCode);

    @Modifying
    @Query("update PPStorage ps set ps.status=1 where ps.storageCode=?1")
    int openStorages(String storageCode);

    @Query("select ps from PPStorage ps where  ps.storageCode=?1")
    PPStorage findStorageByStorageCode(String storageCode);

    @Modifying
    @Query("update PPStorage ps set ps.locationInventoryStatus=1,ps.palletCode=?1 where ps.storageCode=?2")
    int finishInBound(String palletCode, String storageCode);

    @Modifying
    @Query("update PPStorage ps set ps.status=?1 where ps.id=?2")
    int changeStorageStatusById(Integer status, Integer storageId);


    @Modifying
    @Query("update PPStorage ps set ps.locationInventoryStatus=0,ps.palletCode=?1 where ps.storageCode=?2")
    void finishInBoundNullPallet(String palletCode, String storageCode);

    PPStorage findPPStorageByPalletCode(String palletCode);
}
